#!/bin/bash
# chkconfig: 2345 55 25
# description: xyserver Cloud Service

### BEGIN INIT INFO
# Provides:          bawei
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts xyserver
# Description:       starts the xyserver
### END INIT INFO

source dist/app.conf
JAR_NAME=adsense*.jar # jar包的名字


panel_status()
{
    echo -e "查看当前状态... \c";
}

start_application() {
  echo "开始启动等待30秒"
                sleep 30
                echo "完成等待"
    cd ${WWW_HOME}
    nohup java -jar ${JAR_NAME} --server.port=${APP_PORT} >adsense_$(date +%Y%m%d%H%M%S).log 2>&1 &
}

stop_application() {
    echo 'stopping the 咸鱼....'
    PID=`ps ax |grep ${APP_PORT}|grep -v grep|awk '{print $1}'`
    echo ${PID}
    if [[ ! -z "$PID" ]]; then
    kill -15 $PID
    else
     echo '咸鱼 is not running'
    fi
}
install_application(){
     mkdir -p ${WWW_HOME}
#    部署时检测JDK有没有安装，如果没有的话，先安装JDK
    if [ -x /usr/bin/java ]; then
        echo "java has installed!"
    else
        echo "install java"
        yum install java -y
    fi

    echo "开始安装应用！！"
    stop_application
    cd ${WWW_HOME}

    rm -rf *.jar
    rm -rf static
    cd ${UZ_HOME}
    ls -a

    cp -rf ${JAR_NAME} ${WWW_HOME}/
    cp -rf static ${WWW_HOME}/static
    start_application
    rm -rf ${UZ_HOME}/*.jar
    rm -rf ${UZ_HOME}/static


}
case "$1" in
        'start')
                start_application
                ;;
        'stop')
                stop_application
                ;;
        'install')
                install_application
        ;;
        'restart')
                stop_application

                start_application
                ;;
        'status')
                panel_status
                ;;
        'default')

                ;;
        *)
                echo "Usage: /etc/init.d/xyserver {start|stop|restart|reload|default}"
        ;;
esac
